package com.cn.http.command.impl;

import cn.com.allunion.sms.pojo.Smsrecord;
import com.cn.http.command.AbstractSmsCommand;
import com.cn.http.service.SmsSendService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据电话号码所述运营商自动匹配运营商发送命令
 * Created by YangJie on 2016/4/21.
 */
public class AutoGroupSmsCommand extends AbstractSmsCommand {
    private final static Logger LOGGER = LoggerFactory.getLogger(ChuangLanSmsCommand.class) ;

    /**
     * 短信发送服务map
     */
    private Map<String, SmsSendService> smsSendServiceMap ;

    protected AutoGroupSmsCommand(String joinNumMark) {
        super(joinNumMark);
    }

    @Override
    public void execute(List<Smsrecord> smsrecords) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("---> auto 默认短信发送命令激活");
        }
        Map<String, List<Smsrecord>> smsrecordListMap = filterNeedRecord(smsrecords) ;
        if (MapUtils.isNotEmpty(smsrecordListMap)) {
            for (Map.Entry<String, List<Smsrecord>> smsrecordListEntry : smsrecordListMap.entrySet()) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("---> 发送运营商渠道代码: {}", smsrecordListEntry.getKey());
                }
                smsSendServiceMap.get(smsrecordListEntry.getKey()).send(smsrecordListEntry.getValue());
            }
        }
    }

    /**
     * 根据注入进来的服务的 key 将短信记录进行分组
     * @param smsrecords 短信记录集合
     * @return 分组结果 key 短信渠道代码 value 短信记录集合
     */
    private Map<String, List<Smsrecord>> filterNeedRecord(List<Smsrecord> smsrecords) {
        if (CollectionUtils.isEmpty(smsrecords)) {
            return null ;
        }

        Map<String, List<Smsrecord>> smsrecordsMap = new HashMap<>(3) ;

        List<Smsrecord> smsrecordList = null ;
        for (String key : smsSendServiceMap.keySet()) {
            smsrecordList = myNeedObjects(smsrecords, key) ;
            if (CollectionUtils.isNotEmpty(smsrecordList)) {
                smsrecordsMap.put(key, smsrecordList);
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("---> 分组运营商渠道代码: {}， 短信记录数量: {}", key, smsrecordList.size());
            }
        }

        return smsrecordsMap;
    }

    /**
     * 根据给定的 joinNumMarked 短信通道标识，获取数据该短信通道的短信记录集合
     * @param smsRecords 要发送的短信记录集合
     * @param joinNumMarked 短信通道标识
     * @return SmsOperator.joinNum 等于给定的短信通道标识marked的所有短信记录集合
     */
    private List<Smsrecord> myNeedObjects(List<Smsrecord> smsRecords, String joinNumMarked) {

        if (StringUtils.isBlank(joinNumMarked) || CollectionUtils.isEmpty(smsRecords)) {
            return null ;
        }

        // 数据 joinNumMarked 标识短信通道的所有短信记录集合
        List<Smsrecord> needSmsRecords = new ArrayList<>() ;

        for (Smsrecord sd: smsRecords) {
            if (joinNumMarked.equals(sd.getSmsOperator().getJoinNum())) { // 属于这个短信通道的短信
                needSmsRecords.add(sd);
            }
        }
        // 移除
        smsRecords.remove(needSmsRecords) ;

        return needSmsRecords ;
    }

    public Map<String, SmsSendService> getSmsSendServiceMap() {
        return smsSendServiceMap;
    }

    public void setSmsSendServiceMap(Map<String, SmsSendService> smsSendServiceMap) {
        this.smsSendServiceMap = smsSendServiceMap;
    }
}
